package com.btpj.hospital_server.service;

import com.btpj.hospital_server.dto.param.HospitalClinicParam;
import com.btpj.hospital_server.entity.HospitalClinic;
import com.btpj.hospital_server.mapper.HospitalClinicMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 医院诊室Service
 *
 * @author BTPJ  2020/8/26
 */
@Service
public class HospitalClinicService {

    @Resource
    private HospitalClinicMapper hospitalClinicMapper;

    /**
     * 是否存在指定ID的诊室
     *
     * @param id 诊室的编号
     * @return 是否存在指定ID的诊室
     */
    public boolean existClinic(Long id) {
        return hospitalClinicMapper.selectByPrimaryKey(id) != null;
    }

    /**
     * 添加诊室信息
     *
     * @param param HospitalClinicParam
     * @return 是否添加成功
     */
    public boolean addClinic(HospitalClinicParam param) {
        HospitalClinic hospitalClinic = new HospitalClinic();
        BeanUtils.copyProperties(param, hospitalClinic);
        hospitalClinic.setGmtCreate(new Date());
        hospitalClinic.setGmtModified(new Date());
        return hospitalClinicMapper.insertSelective(hospitalClinic) > 0;
    }

    /**
     * 修改诊室信息
     *
     * @param id    诊室编号
     * @param param HospitalClinicParam
     * @return 是否修改成功
     */
    public boolean updateClinic(Long id, HospitalClinicParam param) {
        HospitalClinic hospitalClinic = hospitalClinicMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(param, hospitalClinic);
        hospitalClinic.setGmtModified(new Date());
        return hospitalClinicMapper.updateByPrimaryKeySelective(hospitalClinic) > 0;
    }

    /**
     * 删除诊室信息
     *
     * @param id 诊室编号
     * @return 是否删除成功
     */
    public boolean deleteClinic(Long id) {
        return hospitalClinicMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 获取诊室信息
     *
     * @param id 诊室编号
     * @return 诊室信息
     */
    public HospitalClinic getClinicDetails(Long id) {
        return hospitalClinicMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取指定编号门诊对应的诊室列表
     *
     * @param outpatientId 门诊编号
     * @return 指定编号门诊对应的诊室列表
     */
    public List<HospitalClinic> getClinicListByOutpatientId(Long outpatientId) {
        return hospitalClinicMapper.selectByOutpatientId(outpatientId);
    }
}
